/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.edu.pucp.cpnc.mm.view;

import javax.swing.JDialog;
import javax.swing.SwingUtilities;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.DefaultFormatter;
import pe.edu.pucp.cpnc.mm.bean.Map;
import pe.edu.pucp.cpnc.mm.manager.CmdManager;


/**
 *
 * @author Kei,Luis
 */
public class NewMapPanel extends javax.swing.JPanel {
    private Map map;
    private boolean nameOk;
    private boolean sizeOk;
    
    /**
     * Creates new form NewMapPanel
     */
    public NewMapPanel() {
        initComponents();
        DefaultFormatter dF = new DefaultFormatter();
        DefaultFormatter dF2 = new DefaultFormatter();
        dF.setCommitsOnValidEdit(true);
        dF2.setCommitsOnValidEdit(true);
        txtName.setFormatterFactory(new DefaultFormatterFactory(dF));
        txtWidth.setFormatterFactory(new DefaultFormatterFactory(dF2));
        txtHeight.setFormatterFactory(new DefaultFormatterFactory(dF2));
        txtWidth.setText("50");
        txtHeight.setText("50");
        
        map = null;
    }
    
    public Map getMap(){
        return map;
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        btnCancel = new javax.swing.JButton();
        btnCreate = new javax.swing.JButton();
        cbSizes = new javax.swing.JComboBox();
        txtName = new javax.swing.JFormattedTextField();
        lblErrorName = new javax.swing.JLabel();
        lblErrorSize = new javax.swing.JLabel();
        txtWidth = new javax.swing.JFormattedTextField();
        txtHeight = new javax.swing.JFormattedTextField();

        jLabel1.setText("New Map Settings");

        jLabel2.setText("Size:");

        jLabel3.setText("Width");

        jLabel4.setText("Height");

        jLabel5.setText("Name:");

        btnCancel.setText("Cancel");
        btnCancel.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnCreate.setText("Create");
        btnCreate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateActionPerformed(evt);
            }
        });

        cbSizes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Small", "Medium", "Large", "Very Large", "Gigantic", "Custom" }));
        cbSizes.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbSizesItemStateChanged(evt);
            }
        });

        txtName.setFormatterFactory(new DefaultFormatterFactory(null));
        txtName.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                txtNamePropertyChange(evt);
            }
        });

        lblErrorName.setForeground(new java.awt.Color(255, 51, 51));
        lblErrorName.setText(" ");

        lblErrorSize.setForeground(new java.awt.Color(255, 51, 51));
        lblErrorSize.setText(" ");

        txtWidth.setEditable(false);
        txtWidth.setText("50");
        txtWidth.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                txtWidthPropertyChange(evt);
            }
        });

        txtHeight.setEditable(false);
        txtHeight.setText("50");
        txtHeight.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                txtHeightPropertyChange(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblErrorSize, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnCreate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancel))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(lblErrorName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cbSizes, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(txtWidth, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(21, 21, 21)
                                        .addComponent(jLabel4)
                                        .addGap(18, 18, 18)
                                        .addComponent(txtHeight, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblErrorName)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cbSizes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(txtWidth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtHeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                .addComponent(lblErrorSize)
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnCreate))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
       
        JDialog parent = (JDialog)SwingUtilities.getRoot(this);
        parent.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnCreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateActionPerformed
        
        String name = txtName.getText();
        int width = Integer.parseInt(txtWidth.getText());
        int height = Integer.parseInt(txtHeight.getText());
        
        Map newMap = new Map(width,height, name, MainFrame.resources.getAvailableTiles().get(2));
        CmdManager.addPlayer(1, 13132900, 0, "Bando1", "Bando1", newMap);
        CmdManager.addPlayer(2, 5268680, 0, "Bando2", "Bando2", newMap);
        CmdManager.addPlayer(3, 7884920, 0, "Bando3", "Neutral", newMap);
        CmdManager.showPlayers(newMap);
       
       NewMapDialog parent = (NewMapDialog)SwingUtilities.getRoot(this);
       this.map = newMap;
       parent.setVisible(false);
    }//GEN-LAST:event_btnCreateActionPerformed

    private void cbSizesItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbSizesItemStateChanged
        int i = cbSizes.getSelectedIndex() +1;
        if(i==cbSizes.getItemCount()){
            txtHeight.setEditable(true);
            txtWidth.setEditable(true);
        }else{
            txtHeight.setEditable(false);
            txtWidth.setEditable(false);
            txtHeight.setText("" + 50*i);
            txtWidth.setText("" + 50*i);
        }
        txtWidthPropertyChange(null);
        
    }//GEN-LAST:event_cbSizesItemStateChanged

    private void txtNamePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_txtNamePropertyChange
        if(evt.getPropertyName()==null || !evt.getPropertyName().equals("value"))
            return;
        String name = txtName.getText();
        
        if(name.equals("") || name.length()>50){
            lblErrorName.setText("Field cannot be empty or longer than 50 characters.");
            nameOk = false;
            btnCreate.setEnabled(nameOk && sizeOk);
        }else if(!name.matches("^[\\p{Alnum}]+$")){
            lblErrorName.setText("Field contains illegal characters.");
            nameOk = false;
            btnCreate.setEnabled(nameOk && sizeOk);
        }
        else{
            lblErrorName.setText(" ");
            nameOk = true;
            btnCreate.setEnabled(nameOk && sizeOk);
        }
    }//GEN-LAST:event_txtNamePropertyChange

    private void txtWidthPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_txtWidthPropertyChange
        try{
            int width = Integer.parseInt(txtWidth.getText());
            int height = Integer.parseInt(txtHeight.getText());
            if(width<10 || width>300 || height<10 || height>300){
                lblErrorSize.setText("Fields must be numbers between 10 and 300.");
                sizeOk = false;
                btnCreate.setEnabled(nameOk && sizeOk);
            }else{
                lblErrorSize.setText(" ");
                sizeOk = true;
                btnCreate.setEnabled(nameOk && sizeOk);
            }
        }catch(NumberFormatException ex){
            lblErrorSize.setText("Fields must be numbers between 10 and 300.");
            sizeOk=false;
            btnCreate.setEnabled(nameOk && sizeOk);
        }
    }//GEN-LAST:event_txtWidthPropertyChange

    private void txtHeightPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_txtHeightPropertyChange
        txtWidthPropertyChange(evt);
    }//GEN-LAST:event_txtHeightPropertyChange


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnCreate;
    private javax.swing.JComboBox cbSizes;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel lblErrorName;
    private javax.swing.JLabel lblErrorSize;
    private javax.swing.JFormattedTextField txtHeight;
    private javax.swing.JFormattedTextField txtName;
    private javax.swing.JFormattedTextField txtWidth;
    // End of variables declaration//GEN-END:variables
}
